//初始化插件
var articleEditor_inputEditor;
var articleMessagePageNum = 0;
var onePageDataCount = 25;

KindEditor.ready(function(K) {
	var editor = K.create('textarea[id="inputTextArea"]', {
		//cssPath : 'plugins/editor/plugins/code/prettify.css',
		/*uploadJson : '/uploads/upload_json.jsp',*/
		uploadJson : '/uploads/upload_json.jsp',
		//fileManagerJson : 'plugins/editor/jsp/file_manager_json.jsp',
		allowFileManager : true,
		resizeType : 0,
		minWidth: 900,
		minHeight: 130,
		items : ['emoticons', '|', 'image']
	});
	prettyPrint();
	articleEditor_inputEditor = editor;
});
$(function() {
	$("#submitReplay").bind({
		"click": checkAndSubmitReplay
	});
	$("#loginButton").click(doLogin);
	$("#moreMessage").click(loadMoreMessage);
});
function getReply(){
	$("#submitReplay").focus();
}

function userHadLogin() {
	if ($.cookie("userHadLogin") && $.cookie("userHadLogin") === "true") {
		return true;
	}
	return false;
}

function checkAndSubmitReplay() {
	if (!userHadLogin()) {
		openChatLogin("请先登录");
		return ;
	}
	var articleId = $("#articleId").val();
	var articleType = $("#article_type").val();
	var content = articleEditor_inputEditor.html();
	if (!CHECK.isEmpty(articleId) && !CHECK.isEmpty(articleType) && !articleEditor_inputEditor.isEmpty("inputTextArea")) {
		articleEditor_inputEditor.html("");
		$.ajax({
			url: WEBROOT + "/play/article/addArticleMessage",
			type: "post",
			dataType: "json",
			data: {
				"content": content,
				"article_type" : articleType,
				"pId" : articleId
			},
			success: function(res) {
				if (res.result == AjaxMsgConstants.SUCCESS) {
					var mi = res.mi;
					var appendNode = $("#moreMessageBox");
					appendNode.css("display", "block");
					htmlStr = '<div class="appendDetailAtLocal" style="width: 960px;height: auto; border-top: 1px solid #dadada; margin-top: 20px; margin-bottom: 20px;">' +
				  				  '<div style="float: left;width: 50px;height: 50px;border: 0 none;border-radius: 4px 4px 4px 4px; overflow: hidden; margin-top: 20px; ">'+
								  	  '<img style="width:50px;height: 50px; " src="'+res.author.userLogoUrl+'">' +
								  '</div>' +
								  '<div style="float: left;width: 870px;height: auto;margin-top: 20px;margin-left: 20px;">' +
									  '<div style="width: 870px;height: 20px;margin-bottom: 10px;line-height: 20px;font-size: 15px;color: #946652">'+res.author.authorName+'</div>' +
									  '<div style="width: 870px;height: auto;margin-bottom: 10px;line-height: 20px;font-size: 15px;">'+content+'</div>' +
									  '<div style="width: 870px;height: 20px;margin-bottom: 10px;line-height: 20px;font-size: 15px;">' +
										   '<div style="float: left;padding: 0 5px 0 5px;width: auto;height: 20px;line-height: 20px;font-size: 15px;color: #9B9B9B;">刚刚</div>' +
										   '<div style="float: left;padding: 0 5px 0 5px;width: auto;height: 20px;line-height: 20px;font-size: 15px;color: #946652;margin-left: 10px; cursor: pointer;">' +
										  	   '<a href="#submitReplay">评论</a>'+
										   '</div>' +
									   '</div>' +
							   	  '</div>' +
							  	  '<div style="clear:both;"></div>' +
							  '</div>';
					appendNode.append(htmlStr);	
				} else {
					alert("评论失败");
				}
			}
		});
	} else {
		alert("评论内容不能为空");
	}
}

function openChatLogin(message){
	$("#loginStorey").css({"display": "block"});
	if (message && "" != message) {
		$("#login_before_error_message").html(message);
	}
}

function doLogin() {
	var userLoginName = $("#userLoginName").val();
	var userLoginPassword = $("#userLoginPassword").val();
	if (CHECK.isEmail(userLoginName) && CHECK.isPassword(userLoginPassword)){
		$.ajax({
			type: 'post',
			url: WEBROOT + '/user/ajaxLogin',
			data: {
				"email" : userLoginName,
				"password" : userLoginPassword
			},
			dataType: 'json',
			success: function(reultMap){
				if(reultMap.result == AjaxMsgConstants.SUCCESS){
					closeLoginPage();
					doInsertCookie(userLoginName,userLoginPassword);
					var date = new Date();
					// 设置过期时间为半个小时
					date.setTime(date.getTime() + 30 * 60 * 1000);
					$.cookie("userHadLogin", true, {path: "/",expires: date});
					alert("登陆成功");
				} else {
					alert("登陆失败");
				}
			}
		});
	} else {
		alert("输入的信息有误");
	}
}

function doInsertCookie(userLoginName,userLoginPassword){
	if($.cookie("userLoginName") != null){
		$.cookie("userLoginName",null);
	}
	if ($.cookie("userLoginPassword") != null) {
		$.cookie("userLoginPassword",null);
	}
	$.cookie("userLoginName",userLoginName,{expires:7});
	$.cookie("userLoginPassword",userLoginPassword,{expires:7});
}

/**
 * 加载更多的评论
 */
function loadMoreMessage() {
	$.ajax({
		url: WEBROOT + "/play/article/getArticleDetails",
		type: "post",
		dataType: "json",
		data: {
			"articleId": $("#articleId").val(),
			"pageNum": articleMessagePageNum 
		},
		success: function(res) {
			if (res.result == AjaxMsgConstants.SUCCESS) {
				$(".appendDetailAtLocal").remove();
				if (res.details.length > 0) {
					if (res.details.length < onePageDataCount) {
						$("#moreMessage").css("display", "none");
					} else if (res.details.length == onePageDataCount) {
						articleMessagePageNum ++;
					}
					appendMoreMessageToPage(res.details);
				} else {
					$("#moreMessage").css("display", "none");
				}
			} else {
				alert("数据异常,请稍后重试 ");
			}
		}
	});
}

function appendMoreMessageToPage(details) {
	var dataLength = details.length;
	var appendNode = $("#moreMessageBox");
	appendNode.css("display", "block");
	var htmlStr = '';
	 
	for (var i = 0; i < dataLength; i++) {
		detail = details[i];
		var createTime = new Date();
		createTime.setTime(detail.msgCreateDttm * 1000);
		htmlStr = '<div style="width: 960px;height: auto; border-top: 1px solid #dadada; margin-top: 20px; margin-bottom: 20px;">' +
	  				  '<div style="float: left;width: 50px;height: 50px;border: 0 none;border-radius: 4px 4px 4px 4px; overflow: hidden; margin-top: 20px; ">'+
					  	  '<img style="width:50px;height: 50px; " src="'+detail.author.userLogoUrl+'">' +
					  '</div>' +
					  '<div style="float: left;width: 870px;height: auto;margin-top: 20px;margin-left: 20px;">' +
						  '<div style="width: 870px;height: 20px;margin-bottom: 10px;line-height: 20px;font-size: 15px;color: #946652">'+detail.author.authorName+'</div>' +
						  '<div style="width: 870px;height: auto;margin-bottom: 10px;line-height: 20px;font-size: 15px;">'+detail.msgContent+'</div>' +
						  '<div style="width: 870px;height: 20px;margin-bottom: 10px;line-height: 20px;font-size: 15px;">' +
							   '<div style="float: left;padding: 0 5px 0 5px;width: auto;height: 20px;line-height: 20px;font-size: 15px;color: #9B9B9B;">'+createTime.format("yyyy-MM-dd hh:mm:ss")+'</div>' +
							   '<div style="float: left;padding: 0 5px 0 5px;width: auto;height: 20px;line-height: 20px;font-size: 15px;color: #946652;margin-left: 10px; cursor: pointer;">' +
							  	   '<a href="#submitReplay">评论</a>'+
							   '</div>' +
						   '</div>' +
				   	  '</div>' +
				  	  '<div style="clear:both;"></div>' +
				  '</div>';	
		appendNode.append(htmlStr);		  
	}
}